package com.itheima.controller;


import com.itheima.Service.UserService;
import com.itheima.pojo.User;
import com.itheima.pojo.query.PageQuery;
import com.itheima.pojo.vo.PageResult;
import com.itheima.pojo.vo.Result;
import com.itheima.pojo.vo.UserLoginVo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserConstroller {

    //加final
    private final UserService userService;

    /**
     * 登录
     * 1)接收参数
     * 2)查询数据库对应账号密码的用户
     * 3)查得到,登录成功,生成令牌,并颁发令牌
     * 4)查不到,登录失败,提示错误信息
     */

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        UserLoginVo vo=userService.login(user);
        Result result= new Result();
        if (vo==null){
            result.setMsg("账号或密码错误");
            result.setCode(404);
            return result;
        }
        //响应结果封装
        result.setData(vo);
        result.setCode(200);
        return result;
    }



    /**
     * 分页查询 PageQuery
     */
    @PostMapping
    public PageResult page(@RequestBody PageQuery<User> pageQuery){

        PageResult pageResult=userService.page(pageQuery);
        return pageResult;
    }

}
